package com.sxm.yiwei.activitis;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.tools.BookPayActivity;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class RangingResultPriceActivity extends BaseActivity implements View.OnClickListener{

    private static final int ACTIVITYTYPE = 100;
    private ImageView priceback;
    private TextView betweendistance,betweenprice,priceaddress,pricedistance,pricelink;
    private Button pricesurebt;
    private Button betweenpricebr;
    private Button cancelbookbt;
    private String id;
    private String inname;
    private String inaddress;
    private String distance;
    private String tellphone1;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private int type;
    private Double disparity;
    private double disparity1;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranging_result_price);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");
        type = sharedPreferences.getInt("type",1);
        initRangPriceView();
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        inname = intent.getStringExtra("inname");
        inaddress = intent.getStringExtra("inaddress");
        distance = intent.getStringExtra("distance");
        tellphone1 = intent.getStringExtra("tellphone1");

        priceaddress.setText(inaddress);
        pricelink.setText(inname+"-"+tellphone1);

        String distwo = String.valueOf(new BigDecimal(distance).divide(new BigDecimal(1), 2, BigDecimal.ROUND_FLOOR));
        Double disaaa = Double.valueOf(distwo);

        disparity = new BigDecimal((disaaa/1000)+"").subtract(new BigDecimal(3.0))
                .multiply(new BigDecimal(2)).divide(new BigDecimal(1), 2, BigDecimal.ROUND_FLOOR).doubleValue();

        if (Double.valueOf(distwo)/1000>3.0){
            Double sss = Double.valueOf(distwo);
            pricedistance.setText((sss/1000)+"");
            betweendistance.setText(String.valueOf(sss/1000-3));
            betweenprice.setText(disparity+"");
        }else {
            betweenprice.setText("0");
            betweendistance.setText("0");
        }

    }



    private void initRangPriceView() {
        priceback = (ImageView) findViewById(R.id.rangingprice_back);
        betweendistance = (TextView) findViewById(R.id.rangprice_betweendistance);
        betweenprice= (TextView) findViewById(R.id.rangprice_betweenprice);
        priceaddress = (TextView) findViewById(R.id.rangprice_textaddress);
        pricedistance = (TextView) findViewById(R.id.rangprice_textdistance);
        pricelink = (TextView) findViewById(R.id.rangprice_textlinkman);

        betweenpricebr = (Button) findViewById(R.id.rangresult_betweenpricebt);
        cancelbookbt = (Button) findViewById(R.id.rangresult_cancelbookbt);
        backmain = (ImageView) findViewById(R.id.rangingprice_backmain);
        backmain.setOnClickListener(this);
        priceback.setOnClickListener(this);
        betweenpricebr.setOnClickListener(this);
        cancelbookbt.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rangingprice_backmain:
                startActivity(new Intent(RangingResultPriceActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.rangingprice_back:
                finish();
                break;
            case R.id.rangresult_cancelbookbt://取消订单
                AlertDialog.Builder builder = new AlertDialog.Builder(RangingResultPriceActivity.this,R.style.NoBackGroundDialog);
                View view = LayoutInflater.from(RangingResultPriceActivity.this).inflate(R.layout.cancel_servicedialog, null);

                builder.setView(view);
                Button cancelbt = (Button) view.findViewById(R.id.cancel_cancelbt);
                Button surebt = (Button) view.findViewById(R.id.cancel_surebt);
                TextView t2 = (TextView) view.findViewById(R.id.cancel_t2);
                t2.setText("是否取消此订单 ?");
                final AlertDialog dialog = builder.create();
                cancelbt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                surebt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        cancelBook(id);
                        dialog.dismiss();
                    }
                });
                dialog.show();
                // 取消订单

                break;
            case R.id.rangresult_betweenpricebt://补差价

                String price = (String) betweenprice.getText();
                DecimalFormat    df   = new DecimalFormat("######0.00");
                Double aaa = Double.valueOf(price);
                if (type<2000){//一喂员点击补差价
                        payBetweenprice();
                }else {// 发单人点击补差价
                    Double aDouble = Double.valueOf(price);
                    if (aDouble>0){
                        //支付差价
                        Intent intent = new Intent(RangingResultPriceActivity.this,BookPayActivity.class);
                        intent.putExtra("from","between");
                        intent.putExtra("amount",disparity+"");
                        intent.putExtra("id",id);
                        intent.putExtra("alipay", InternetAddressUtis.ALIBETWEENPRICEBOOK);
                        intent.putExtra("yivipay",InternetAddressUtis.BETWEENPRICEBOOK);
                        startActivityForResult(intent,ACTIVITYTYPE);
                    }
                }
                break;
        }
    }
        // 补差价
    private void payBetweenprice() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.YIIVIERBETWEENPRICEBOOK);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        JSONObject object = new JSONObject();
        object.put("orderId",id);
       params.setBodyContent(object.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                finish();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(RangingResultPriceActivity.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    // 取消订单
    private void cancelBook(String id) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url + InternetAddressUtis.CANCELBOOK);
        params.addHeader("Content-Type", "application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        JSONObject j = new JSONObject();
        j.put("orderId", id);
        params.setBodyContent(j.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                JSONObject object = JSON.parseObject(result);
                String status = object.getString("status");

                if (status.equals("1")){
                    Toast.makeText(RangingResultPriceActivity.this, "订单已取消", Toast.LENGTH_SHORT).show();
                    finish();
                }else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(RangingResultPriceActivity.this,R.style.NoBackGroundDialog);
                    View view = LayoutInflater.from(RangingResultPriceActivity.this).inflate(R.layout.cancel_servicedialog, null);

                    builder.setView(view);
                    Button cancelbt = (Button) view.findViewById(R.id.cancel_cancelbt);
                    Button surebt = (Button) view.findViewById(R.id.cancel_surebt);
                    TextView t2 = (TextView) view.findViewById(R.id.cancel_t2);
                    t2.setText("已不能取消订单");
                    final AlertDialog dialog = builder.create();
                    cancelbt.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                    surebt.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                    // 取消订单
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

                ErrType.errType(RangingResultPriceActivity.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
